//
//  noticeView.m
//  HQJFacilitator
//
//  Created by mymac on 2016/10/10.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "NoticeView.h"
@interface NoticeView ()
@property (nonatomic,copy) NSString *vipCount;
@property (nonatomic,copy) NSString *vipSumZH;
@property (nonatomic,strong) UIView *backGroundView;
@end
@implementation NoticeView
-(UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel =  [[UILabel alloc]init];
         _titleLabel.font = [UIFont systemFontOfSize:15 weight:.3];
        [self.backGroundView addSubview:_titleLabel];
    }
    
    
    return _titleLabel;
}
-(UILabel *)NewTitleLabel {
    if (!_NewTitleLabel) {
        _NewTitleLabel = [[UILabel alloc]init];
        _NewTitleLabel.font =[UIFont systemFontOfSize:15 weight:.3];
        [self.backGroundView addSubview:_NewTitleLabel];

    }
    
    
    
    return _NewTitleLabel;
}
-(instancetype)initWithFrame:(CGRect)frame andtitle:(NSDictionary *)dic andType:(NSInteger)type{

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, frame.size.height)];
        self.backGroundView.backgroundColor = [ManagerEngine getColor:@"fff2b2"];
        [self addSubview:self.backGroundView];
       NSLog(@"这个广告条的Dickies:%@",dic);
        if (dic.allKeys.count != 0) {
            if (type == 0) {
                [self lableTitleStr:[NSString stringWithFormat:@"当前会员数%@人,会员ZH值总计%@个",dic[@"count"],dic[@"sumZH"]]];
                
            } else if (type == 1) {
                
                [self lableTitleStr:[NSString stringWithFormat:@"当前服务商%@家,服务商ZH值总计%@个",dic[@"count"],dic[@"sumZH"]]];
            } else {
                //NSString *str;
               // str doubleValue
                
                [self lableTitleStr:[NSString stringWithFormat:@"当前服务商账号有%@个积分,会员有%.2f个积分",dic[@"oribonus"],[dic[@"cusbonus"] doubleValue] ]];
            }

        } else {
            [self lableTitleStr:[NSString stringWithFormat:@""]];

        }
        
        
        
    }
    
    
    
    return self;
}
-(void)lableTitleStr:(NSString *)str {
    
    self.titleLabel.text = str;
    
    NSArray *number = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"."];

    NSMutableAttributedString *attributeString  = [[NSMutableAttributedString alloc]initWithString:str];
    for (int i = 0; i < str.length; i ++) {
        //这里的小技巧，每次只截取一个字符的范围
        NSString *a = [str substringWithRange:NSMakeRange(i, 1)];
        //判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符，从而筛选出符合要求的数字字符的范围NSMakeRange
        if ([number containsObject:a]) {
            [attributeString setAttributes:@{NSForegroundColorAttributeName:[ManagerEngine getColor:@"f54949"],NSFontAttributeName:[UIFont systemFontOfSize:15 weight:.3],NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]} range:NSMakeRange(i, 1)];
        }
        
    }
    //完成查找数字，最后将带有字体下划线的字符串显示在UILabel上
    self.titleLabel.attributedText = attributeString;
  
    CGFloat titleSize = [ManagerEngine setTextWidthStr:str andFont:[UIFont systemFontOfSize:15 weight:.3]];
    self.titleLabel.frame = CGRectMake(15, (44-20)/2, titleSize, 20);
    _timerIntrger = str.length/3;

    if (titleSize+EDGE*2>WIDTH) {
        self.NewTitleLabel.frame =CGRectMake(self.titleLabel.frame.origin.x+titleSize, self.titleLabel.frame.origin.y, titleSize, 20);
        
        self.NewTitleLabel.text = str;
        self.NewTitleLabel.attributedText = attributeString;

        [self animationStar];
    }
    for (NSInteger i=0; i<2; i++) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor orangeColor];
        view.frame = CGRectMake(i%2*(15+WIDTH-30), 0, 15, 44);
        view.backgroundColor = [ManagerEngine getColor:@"fff2b2"];;
        [self.backGroundView addSubview:view];
    }
    
    
    
}
-(void)animationStar {
    
    CGRect titleFrame = self.titleLabel.frame;
    CGRect newTitleFrame = self.NewTitleLabel.frame;
    [UIView animateWithDuration:_timerIntrger delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.titleLabel.frame = CGRectMake(-self.titleLabel.frame.size.width, self.titleLabel.frame.origin.y, self.titleLabel.frame.size.width, 20);
        self.NewTitleLabel.frame = CGRectMake(15, self.NewTitleLabel.frame.origin.y, self.NewTitleLabel.frame.size.width, 20);
        
    } completion:^(BOOL finished) {
        self.titleLabel.frame = titleFrame ;
        self.NewTitleLabel.frame = newTitleFrame ;
        [self animationStar];
    }];
    
}


-(void)dismssPopView {
  
    for (UIView *view in self.backGroundView.subviews) {
    
        [view removeFromSuperview];
    }
    
    [self removeFromSuperview];
    
}
@end
